package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.MapInfoDialog6Binding;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfEntity;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.DialogViewHolder;
import com.swgk.core.util.DateUtils;

public class MapInfoDialog6 extends BaseBottomDialog {

    private TheEnterpriseOfEntity entity;
    private TextView titleView;
    private TextView tvCome;
    private TextView tvAuthYear;
    private TextView tvAddress;
    private TextView tvDistance;
    private TextView companyView;
    private TextView rootLayout;
    private ImageView mIvMapInfoPicture;
    private RelativeLayout rlClose;
    MapInfoDialog6Binding binding;
    private DialogClickCallBack dialogClickCallBack;

    public MapInfoDialog6(Context context, DialogCallBack dialogCallBack) {
        super(context, dialogCallBack);
    }

    public MapInfoDialog6(Context context, int theme, DialogCallBack dialogCallBack) {
        super(context, theme, dialogCallBack);
    }

    @Override
    protected int dialogLayout() {
        return R.layout.map_info_dialog6;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        binding = (MapInfoDialog6Binding) holder.getmBinding();
        titleView = holder.getDialog().findViewById(R.id.title_view);
        tvAuthYear = holder.getDialog().findViewById(R.id.tv_authYear);
        tvAddress = holder.getDialog().findViewById(R.id.tv_address);
        tvCome = holder.getDialog().findViewById(R.id.tv_come);
        tvDistance = holder.getDialog().findViewById(R.id.tv_distance);
        companyView = holder.getDialog().findViewById(R.id.company_view);
        mIvMapInfoPicture = holder.getDialog().findViewById(R.id.iv_map_info_picture);
        rlClose = holder.getDialog().findViewById(R.id.rl_close);
        rlClose.setOnClickListener(v -> MapInfoDialog6.this.cancel());
        rootLayout = holder.getDialog().findViewById(R.id.tv_follow);

        tvCome.setOnClickListener(v -> {
            if (dialogClickCallBack == null) {
                return;
            }
            if (entity == null) {
                return;
            }
            dialogClickCallBack.onClickShop(entity.getSupplier());
//            MapInfoDialog6.this.cancel();
        });
    }

    public TheEnterpriseOfEntity getData() {
        return entity;
    }

    public MapInfoDialog6 setData(TheEnterpriseOfEntity entity) {
        this.entity = entity;
        titleView.setText(entity.getSupplierName());
        tvAddress.setText(entity.getAddressNew());
        tvDistance.setText(entity.getDistance() + "km");
        companyView.setText(entity.getMainBusiness());
        Glide.with(getContext()).load(entity.getLogo()).apply(new RequestOptions().placeholder(R.mipmap.gongsiqiye).fallback(R.mipmap.gongsiqiye).error(R.mipmap.gongsiqiye)).apply(RequestOptions.bitmapTransform(new RoundedCorners(2))).into(mIvMapInfoPicture);
        if (entity.getAuthTime() != 0) {
            tvAuthYear.setText(DateUtils.timestampTo13Date3NoHours(entity.getAuthTime()) + "入驻");
        } else {
            tvAuthYear.setText("暂无数据");
        }
        if (entity.getMainBusiness() != null && !entity.getMainBusiness().equals("")) {
            companyView.setText(entity.getMainBusiness());
        } else {
            companyView.setText("暂无数据");
        }
        if (entity.getLatitude().equals("0") && entity.getLongitude().equals("0")) {//经纬度为0
            if (entity.getAddressNew() != null && !entity.getAddressNew().equals("")) {//地址不为空
                binding.tvAddress.setVisibility(View.VISIBLE);
                binding.ivAddress.setVisibility(View.VISIBLE);
            } else {
                binding.tvAddress.setVisibility(View.GONE);
                binding.ivAddress.setVisibility(View.GONE);
            }
            binding.tvDistance.setVisibility(View.GONE);
        } else {
            if (entity.getAddressNew() != null && !entity.getAddressNew().equals("")) {//地址不为空
                binding.tvAddress.setVisibility(View.VISIBLE);
                binding.ivAddress.setVisibility(View.VISIBLE);
                binding.tvDistance.setVisibility(View.VISIBLE);
            } else {
                binding.tvAddress.setVisibility(View.GONE);
                binding.ivAddress.setVisibility(View.GONE);
                binding.tvDistance.setVisibility(View.GONE);
            }
        }

        rootLayout.setBackgroundResource(entity.getFollow() ? R.drawable.bg_gz_selected : R.drawable.bg_gz_normal);
        rootLayout.setText(entity.getFollow() ? "已关注" : "关注");
        rootLayout.setTextColor(entity.getFollow() ? context.getResources().getColor(R.color.color_969696) : context.getResources().getColor(R.color.color_5792FD));
        rootLayout.setOnClickListener(v ->
                dialogClickCallBack.onClickFollow(MapInfoDialog6.this.entity)
        );
        return this;
    }

    public interface DialogClickCallBack {
        void onClickShop(String shopId);

        default void onClickFollow(TheEnterpriseOfEntity entity) {

        }
    }

    public MapInfoDialog6 setDialogClickCallBack(DialogClickCallBack dialogClickCallBack) {
        this.dialogClickCallBack = dialogClickCallBack;
        return this;
    }

    public void updateText() {
        if (entity.getFollow()) {
            entity.setFollow(false);
            rootLayout.setBackgroundResource(R.drawable.bg_gz_normal);
            rootLayout.setText("关注");
            rootLayout.setTextColor(getContext().getResources().getColor(R.color.new_blue));
        } else {
            entity.setFollow(true);
            rootLayout.setBackgroundResource(R.drawable.bg_gz_selected);
            rootLayout.setText("已关注");
            rootLayout.setTextColor(getContext().getResources().getColor(R.color.color_999999));
        }
    }
}
